package ctrs.controller;

import com.alibaba.fastjson.JSON;
import ctrs.bean.QueryInfo;
import ctrs.bean.Question;
import ctrs.dao.QuestionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @author Akie
 * @create 2021-03-11 13:50
 */
@RestController
public class QuestionController {

    @Autowired
    QuestionDao questionDao;

    @CrossOrigin
    @RequestMapping("/allQuestion")
    public String getAllQuestion(QueryInfo queryInfo){
        System.out.println(queryInfo);
        List<Question> question = questionDao.getAllQuestion("%"+queryInfo.getQuery()+"%");
        HashMap<String , Object> res = new HashMap<>();
        res.put("data",question);
        System.out.println("res:"+res);
        String question_json = JSON.toJSONString(res);
        return question_json;
    }

    @RequestMapping("/addQuestion")
    public String addQuestion(@RequestBody Question question){
        int i = questionDao.addQuestion(question);
        return i>0?"success":"error";
    }
}
